package com.njwb.Swing.Login;

import com.njwb.Util.UtilService;
import com.njwb.pojo.Bill;
import com.njwb.pojo.SupplierBill;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;

public class StatisticsPanel extends JPanel {
    private UtilService utilService = new UtilService();

    private JRadioButton supplierRadio;
    private JRadioButton productRadio;
    private JTable table;
    private DefaultTableModel tableModel;

    public StatisticsPanel() {
        setLayout(new BorderLayout(5, 5));
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 顶部单选按钮面板
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 15, 5));
        topPanel.setBackground(new Color(240, 248, 255)); // 浅蓝色背景
        topPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

        supplierRadio = new JRadioButton("供应商分组查询");
        productRadio = new JRadioButton("商品分组查询");

        Font radioFont = new Font("宋体", Font.BOLD, 14);
        supplierRadio.setFont(radioFont);
        productRadio.setFont(radioFont);

        ButtonGroup group = new ButtonGroup();
        group.add(supplierRadio);
        group.add(productRadio);

        topPanel.add(supplierRadio);
        topPanel.add(productRadio);

        add(topPanel, BorderLayout.NORTH);

        // 表格
        tableModel = new DefaultTableModel();
        table = new JTable(tableModel);
        table.setRowHeight(28);
        table.setFont(new Font("宋体", Font.PLAIN, 14));
        table.getTableHeader().setFont(new Font("宋体", Font.BOLD, 14));
        table.getTableHeader().setBackground(new Color(200, 220, 240));
        table.setSelectionBackground(new Color(173, 216, 230));

        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        add(scrollPane, BorderLayout.CENTER);

        // 单选按钮事件
        supplierRadio.addActionListener(e -> loadSupplierStatistics());
        productRadio.addActionListener(e -> loadProductStatistics());

        // 默认显示供应商统计
        supplierRadio.setSelected(true);
        loadSupplierStatistics();
    }

    // 供应商分组统计
    private void loadSupplierStatistics() {
        String[] columnNames = {"编号", "供应商名字", "总交易额", "商品种类", "商品总数量"};
        tableModel.setDataVector(new Object[0][0], columnNames);

        ArrayList<SupplierBill> list = utilService.getSupplierStatistics();
        for (SupplierBill sb : list) {
            tableModel.addRow(new Object[]{
                    sb.getSupplier_id(),
                    sb.getName(),
                    sb.getSum(),
                    sb.getClas(),
                    sb.getCount()
            });
        }
        adjustColumnWidth();
    }

    // 商品分组统计
    private void loadProductStatistics() {
        String[] columnNames = {"商品名字", "交易数量", "交易金额", "供应商名字"};
        tableModel.setDataVector(new Object[0][0], columnNames);

        ArrayList<Bill> list = utilService.getAllBill();
        for (Bill b : list) {
            tableModel.addRow(new Object[]{
                    b.getProduct_name(),
                    b.getQuantity(),
                    b.getAmount(),
                    b.getSupplier_name()
            });
        }
        adjustColumnWidth();
    }

    // 调整列宽
    private void adjustColumnWidth() {
        for (int i = 0; i < table.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setPreferredWidth(120);
        }
    }
}
